package com.bosssoft.boss_exam_db.service.busines.impl;

import com.bosssoft.boss_exam_db.entity.ApplicantQuestionScore;
import com.bosssoft.boss_exam_db.mapper.ApplicantQuestionScoreMapper;
import com.bosssoft.boss_exam_db.service.base.impl.BaseServiceImpl;
import com.bosssoft.boss_exam_db.service.busines.ApplicantQuestionScoreService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
@Slf4j
public class ApplicantQuestionScoreServiceImpl extends BaseServiceImpl<ApplicantQuestionScore> implements ApplicantQuestionScoreService {

    //注入ApplicantQuestionScoreMapper
    @Autowired
    private ApplicantQuestionScoreMapper applicantQuestionScoreMapper;

    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/3/27
     * @Description 批量添加试卷答题
     * @Param [applicantQuestionScores]
     * @Return java.lang.Boolean
     * @Exception //TODO
     * @since JDK 1.8
     **/
    @Override
    public Boolean insertApplicantQuestionScoreList(List<ApplicantQuestionScore> applicantQuestionScores) {
        try {
            applicantQuestionScores.stream().forEach(applicantQuestionScore -> {
                applicantQuestionScoreMapper.insertSelective(applicantQuestionScore);
            });
        }catch (RuntimeException e){
            return false;
        }
       return true;
    }

    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/3/29
     * @Description 根据条件查询未修改的卷子的用户答题
     * @Param [examId, examPaperId, applicantId, code] 考试ID 试卷ID 应聘者ID 是否修改标识
     * @Return void
     * @Exception //TODO
     * @since JDK 1.8
     **/
    @Override
    public  List<ApplicantQuestionScore> queryNoCheckExamPaper(String examId, String examPaperId, String applicantId, Integer code) {
        Example example = new Example(ApplicantQuestionScore.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("examId",examId);
        criteria.andEqualTo("examPaperId",examPaperId);
        criteria.andEqualTo("applicantId",applicantId);
        criteria.andEqualTo("checked",false);
        criteria.andEqualTo("deleted",false);
        List<ApplicantQuestionScore> applicantQuestionScores = this.applicantQuestionScoreMapper.selectByExample(example );
        return applicantQuestionScores;
    }

    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/4/8
     * @Description 批量插入阅卷记录
     * @Param [applicantQuestionScores]
     * @Return java.lang.Integer
     * @Exception
     * @since JDK 1.8
     **/
    @Override
    public Integer insertBashCheckRecod(List<ApplicantQuestionScore> applicantQuestionScores) {
        return this.applicantQuestionScoreMapper.inserBasth(applicantQuestionScores);
    }

    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/3/30
     * @Description 阅卷官对一份卷子的阅卷记录的保存   根据考试ID 试卷ID 考试人ID 更新阅卷记录
     * @Param [applicantQuestionScores]
     * @Return java.lang.Integer
     * @Exception
     * @since JDK 1.8
     **/
    @Override
    public Integer updateCheckRecod(List<ApplicantQuestionScore> applicantQuestionScores) {
        applicantQuestionScores.stream().forEach(item->{
            Example example = new Example(ApplicantQuestionScore.class);
            Example.Criteria criteria = example.createCriteria();
            if(StringUtils.isEmpty(item.getExamId())){
                return;
            }
            if(StringUtils.isEmpty(item.getExamPaperId())){
                return;
            }
            if(StringUtils.isEmpty(item.getApplicantId())) {
                return;
            }
            if(StringUtils.isEmpty(item.getQuestionId())) {
                return;
            }
            criteria.andEqualTo("examId",item.getExamId());
            criteria.andEqualTo("examPaperId",item.getExamPaperId());
            criteria.andEqualTo("applicantId",item.getApplicantId());
            criteria.andEqualTo("questionId",item.getQuestionId());
            ApplicantQuestionScore updateObj = new ApplicantQuestionScore();
            // 提交阅卷官id
            updateObj.setAdminId(item.getAdminId());
            // 提交阅卷官评价
            updateObj.setAdminNode(item.getAdminNode());
            // 提交该题得分
            updateObj.setApplicantScore(item.getApplicantScore());
            // 提交答案评级
            updateObj.setAnswerStar(item.getAnswerStar());
            // 设置为已阅卷
            updateObj.setChecked(true);
            // 保存阅卷记录
            try {
                this.applicantQuestionScoreMapper.updateByExampleSelective(updateObj,example);
            }catch (RuntimeException e){
                System.out.println("出问题了");
                e.printStackTrace();
            }

        });
        return 1;
    }

//    @Override
//    public Integer updateCheckRecod(List<ApplicantQuestionScore> applicantQuestionScores) {
//
//        Example example = new Example(ApplicantQuestionScore.class);
//        Example.Criteria criteria = example.createCriteria();
////        fixme 空校验 暂时未做
//        String examId =null;
//        String examPapeId =null;
//        String applicantId = null;
//        String questionId = null;
//
//        List<ApplicantQuestionScore> applicantQuestionScoreList = applicantQuestionScores.stream().map(item -> {
//            ApplicantQuestionScore updateObj = new ApplicantQuestionScore();
//            // 提交阅卷官id
//            updateObj.setAdminId(item.getAdminId());
//            // 提交阅卷官评价
//            updateObj.setAdminNode(item.getAdminNode());
//            // 提交该题得分
//            updateObj.setApplicantScore(item.getApplicantScore());
//            // 提交答案评级
//            updateObj.setAnswerStar(item.getAnswerStar());
//            // 设置为已阅卷
//            updateObj.setChecked(true);
//            return updateObj;
//        }).collect(Collectors.toList());
//        int record = this.applicantQuestionScoreMapper.updateBasth(applicantQuestionScoreList);
//        return record;
//    }
}
